/*
Copyright (c) [2019年5月1日] [吴超]
[MBT_studio] is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:
		 http://license.coscl.org.cn/MulanPSL2
THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
*/
#include "stdafx.h"
#include "Px物体形状创建.h"



physx::PxConvexMesh* f_Px_创建凸包碰撞网格(physx::PxPhysics* phy, physx::PxCooking* Cooking, const S_三角Mesh& mesh) {
	physx::PxConvexMeshDesc 网格描述;
	网格描述.points.count = mesh.m_vNum;
	网格描述.points.stride = sizeof(vec3);
	网格描述.points.data = mesh.m_vert;
	网格描述.flags = physx::PxConvexFlag::eCOMPUTE_CONVEX;
	网格描述.flags = physx::PxConvexFlag::eGPU_COMPATIBLE;


	physx::PxDefaultMemoryOutputStream buf;
	if (Cooking->cookConvexMesh(网格描述, buf)) {
		physx::PxDefaultMemoryInputData id(buf.getData(), buf.getSize());
		return phy->createConvexMesh(id);
	}

	return 0;

}

physx::PxTriangleMesh* f_Px_三角碰撞网格(physx::PxPhysics* phy, physx::PxCooking* Cooking, const S_三角Mesh& mesh, bool 动态) {
	if (动态) {
		physx::PxTolerancesScale scale;
		physx::PxCookingParams params(scale);
		// disable mesh cleaning - perform mesh validation on development configurations
		params.meshPreprocessParams |= physx::PxMeshPreprocessingFlag::eDISABLE_CLEAN_MESH;
		// disable edge precompute, edges are set for each triangle, slows contact generation
		params.meshPreprocessParams |= physx::PxMeshPreprocessingFlag::eDISABLE_ACTIVE_EDGES_PRECOMPUTE;
		// lower hierarchy for internal mesh
		params.meshCookingHint = physx::PxMeshCookingHint::eCOOKING_PERFORMANCE;

		Cooking->setParams(params);
	}


	physx::PxTriangleMeshDesc 网格描述;

	网格描述.points.count = mesh.m_vNum;
	网格描述.points.stride = sizeof(vec3);
	网格描述.points.data = mesh.m_vert;

	网格描述.triangles.count = mesh.m_iNum;
	网格描述.triangles.stride = sizeof(uvec3);
	网格描述.triangles.data = mesh.m_index;
	//网格描述.flags;

	//for (uint32 i = 0; i < mesh.m_vNum; ++i) cout << mesh.m_vert[i].z << endl;
	//for (uint32 i = 0; i < mesh.m_iNum; ++i) cout << mesh.m_index[i] << endl;
	physx::PxDefaultMemoryOutputStream buf;
	physx::PxTriangleMeshCookingResult::Enum result;


	//auto& ic = phy->getPhysicsInsertionCallback();
	bool status = Cooking->cookTriangleMesh(网格描述, buf, &result);
	//physx::PxTriangleMesh* m = Cooking->createTriangleMesh(网格描述, ic, &result);
	if (!status)
		return NULL;


	physx::PxDefaultMemoryInputData readBuffer(buf.getData(), buf.getSize());
	return phy->createTriangleMesh(readBuffer);


}







